/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.app.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.entity.*
import javafx.scene.input.*
import javafx.scene.paint.*

/**
 *
 *
 * @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
 */
class BenchmarkSample : GameApplication() {
	
	override fun initSettings(settings: GameSettings) {}
	
	override fun initInput() {
		onKeyDown(KeyCode.F, "spawn") { spawn() }
	}
	
	override fun initGameVars(vars: MutableMap<String, Any>) {
		vars["tpf"] = 0.0
	}
	
	private fun spawn() {
		val start = System.nanoTime()
		
		for(i in 0..9999) {
			getGameWorld().create("e", SpawnData(0.0, 0.0))
			
			//var e = new Entity();
			
			//            entityBuilder()
			//                    .at(i * 6, i * 4)
			//                    .buildAndAttach();
		}
		
		val result = System.nanoTime() - start
		System.out.printf("%.3f sec\n", result / 1000000000.0)
	}
	
	override fun initGame() {
		getGameWorld().addEntityFactory(BenchmarkFactory())
		
		val text = getUIFactory().newText("", Color.BLUE, 24.0)
		text.textProperty().bind(getdp("tpf").asString("%.3f"))
		
		addUINode(text, 100.0, 100.0)
	}
	
	override fun onUpdate(tpf: Double) {
		set("tpf", tpf)
	}
	
	// TODO: will fail if not public
	class BenchmarkFactory : EntityFactory {
		
		@Preload(10000)
		@Spawns("e")
		fun newEntity(data: SpawnData): Entity {
			return Entity()
		}
	}
}

fun main(args: Array<String>) {
	GameApplication.launch(BenchmarkSample::class.java, args)
}

